const app = getApp();
import Dialog from "../../../../dist/dialog/dialog";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"",
    member: {},
    device_no:"",

    mini_url: "",
    current_page: 1,
    last_page: 1,
    list: [],
    active:1,

    course_name:"",
    share:0,
    type: null, //1-没绑定、2-绑定了

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split('&');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == 'device_no') {
            this.setData({
              device_no: chil_arr[1]
            });
          }

        }
      }
    }
    if(options.device_no){
      this.setData({device_no:options.device_no})
    }
    this.setData({share:0})
    this.getData()
  },
  delete_card(){
    Dialog.confirm({
      message: '是否删除会员卡',
    })
      .then(() => {
        app._postNetWork({
          url:app.getNetAddresss("plugin.water-machine.frontend.consumer.member-card.unbind"),
          success:resdata=>{
            let {result,msg} = resdata.data;
            app.tips(msg);
            if(result){
              this.getData()
            }
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  onChange({detail}) {
    this.setData({active:detail})
    if (detail == 1) {
      this.get_consumption();
    } else if (detail == 2) {
      this.get_recharge()
    } else if (detail == 3) {
      this.get_gift()
    }
  },

  get_consumption(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.consumer.member-card.consumption'),
      data: {
        page
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result) {
          let list = data.data;
          this.data.current_page = data.current_page;
          this.data.last_page = data.last_page;
          if (page == 1) {
            this.setData({
              list
            })
          } else {
            this.setData({
              list: this.data.list.concat(list)
            })
          }
        } else {
          app.tips(msg);
        }
      }
    })

  },
  get_recharge(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.consumer.member-card.recharge'),
      data: {
        page
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result) {
          let list = data.data;
          this.data.current_page = data.current_page;
          this.data.last_page = data.last_page;
          if (page == 1) {
            this.setData({
              list
            })
          } else {
            this.setData({
              list: this.data.list.concat(list)
            })
          }
        } else {
          app.tips(msg);
        }
      }
    })
  },
  get_gift(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.consumer.member-card.gift'),
      data: {
        page
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result) {
          let list = data.data;
          this.data.current_page = data.current_page;
          this.data.last_page = data.last_page;
          if (page == 1) {
            this.setData({
              list
            })
          } else {
            this.setData({
              list: this.data.list.concat(list)
            })
          }
        } else {
          app.tips(msg);
        }
      }
    })
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.consumer.member-card.index'),
      success: resdata => {
        let {
          data,
          result,
          basic_info,
          msg
        } = resdata.data;
        if (result) {
          let _basic_info = wx.getStorageSync("yz_basic_info") || basic_info;
          let title =_basic_info&&_basic_info.plugin_setting.water_machine_header;
          wx.setNavigationBarTitle({title});
          this.setData({title})
          if(data.device_no == "" && this.data.device_no==""){
            Dialog.alert({
              message: '请扫打水机设备码',
            }).then(() => {
              wx.navigateTo({
                url: data.course_mini_url,
              })
            });

            return;
          }
          if (data.member) {
            this.setData({
              type: 2,
              member:data.member,
            })
          } else {
            this.setData({
              type: 1
            })
          }
          this.setData({
            mini_url: data.course_url,
            course_name:data.course_name
          })
        } else {
          app.tips(msg);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
    this.setData({share:0})

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.current_page>=this.data.last_page){
        return;
      }
      let page = this.data.current_page + 1;

      if (this.data.active == 1) {
        this.get_consumption(page);
      } else if (this.data.active  == 2) {
        this.get_recharge(page)
      } else if (this.data.active  == 3) {
        this.get_gift(page)
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    this.setData({share:1})
    return {
      title: this.data.title,
      path: '/packageI/ecosphere_watercooler/water_use/tie_card/tie_card',
    }
  }
})